<template>
  <MyMavonEditor @edit="edit" operateType="edit" ref="my-mavon-editor" class="create-article" />
</template>

<script>
import { SAVE_USER_INFO } from '__STORE__/operation-type'
import { updateArticle } from '__SERVICE__/article.js'
import { getArticleById } from '__SERVICE__/article.js'
import MyMavonEditor from '__COMPONENTS__/mavon_editor/MyMavonEditor.vue'
//mixin
import paramsDispose from '__CORE__/mixins/paramsDispose.js'
import defaultConfig from '__CONFIG__/base-config.js'
export default {
  name: 'AmendArticle',
  props: ['articleId', 'id'],
  mixins: [paramsDispose],
  components: { MyMavonEditor },
  created() {
    this.getArticleById()//获取文章的数据
  },
  methods: {
    //真正做的事情
    async edit(data) {
      const result = await updateArticle(this.articleId, Object.assign({}, data))
      this.$store.dispatch(SAVE_USER_INFO, {
        access_token: result.data.access_token
      })
      this.$router.push({name: 'Mine'})
    },
    //处理props的id end
    async getArticleById() {
      try {
        const result = await getArticleById(this.articleId)
        const {data: {article}, message} = result
        const {'my-mavon-editor': myMavonEditor} = this.$refs
        article.cover_url = article.cover_url || defaultConfig.ARTICLE_COVER
        myMavonEditor.init(article)

      }catch(e) {
        console.log(e.message)
        this.$Notice.error({
          title: '操作错误',
          desc: 'URL需要是合理值',
          duration: 3000
        })
        this.$router.push({name: 'Home'})
      }
    },
  }
}
</script>

<style>

</style>